﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.Serialization;

namespace Toybox.NicoSharp.Video.MyPage
{
	using Abstracts;

	public class WatchItem : MyPagePrimitives.AbstractMyPageList
	{

		#region [Constructor]

		public WatchItem(CookieCollection cookies = null)
			: base(cookies)
		{
			this.Token = new Token(NicoUrls.VideoTop + "my/fav/user");
			this.Token.Type = TokenType.GlobalHash;
		}

		#endregion [Constructor]


		#region Public Methods

		public ListResult List()
		{
			string url = this.GetUrl("list");
			return this.Get<ListResult>(url);
		}

		public AddResult Add(string user_id)
		{
			return this.Add(new Item(ItemType.User, user_id));
		}

		public AddResult Add(Item item)
		{
			if (this.Token.Expired) this.Token.Update();

			string url = this.GetUrl("add");
			string data = item.ToStringWithoutDesc() + "&token=" + this.Token.Value;
			return this.Post<AddResult>(url, data);
		}

		public ExistResult Exist(string user_id)
		{
			return this.Exist(new Item(ItemType.User, user_id));
		}

		public ExistResult Exist(Item item)
		{
			string url = this.GetUrl("exist");
			string data = item.ToStringWithoutDesc();
			return this.Post<ExistResult>(url, data);
		}

		public DelResult Delete(string user_id)
		{
			return this.Delete(new Item(ItemType.User, user_id));
		}

		public DelResult Delete(string[] user_ids)
		{
			List<Item> list = new List<Item>();

			foreach (string user_id in user_ids)
			{
				list.Add(new Item(ItemType.User, user_id));
			}

			return this.Delete(list.ToArray());
		}

		public DelResult Delete(Item item)
		{
			return this.Delete(new Item[] { item });
		}

		public DelResult Delete(Item[] items)
		{
			if (this.Token.Expired) this.Token.Update();

			string url = this.GetUrl("delete");
			string data = string.Empty;
			foreach (Item item in items)
				data += "&" + item.ToStringForList();

			data += "&token=" + this.Token.Value;

			return this.Post<DelResult>(url, data);
		}

		#endregion Public Methods

		#region Protected Methods

		protected override string GetUrl(string method)
		{
			return NicoUrls.FlApi + "watchitem/" + method;
		}

		#endregion Protected Methods


		#region [SubClass]

		[DataContract]
		public class ListResult : AbstractStatus
		{
			[DataMember]
			public Item[] watchitem;

			#region [SubClass]

			[DataContract]
			public class Item
			{
				[DataMember]
				public ItemType item_type;

				[DataMember]
				public string item_id;

				[DataMember]
				public int create_time;

				[DataMember]
				public int update_time;

				[DataMember]
				public Item_Data item_data;
			}

			[DataContract]
			public class Item_Data
			{
				[DataMember]
				public string id;

				[DataMember]
				public string nickname;

				[DataMember]
				public Uri thumbnail_url;
			}

			#endregion [SubClass]
		}

		[DataContract]
		public class AddResult : AbstractStatus
		{
		}

		[DataContract]
		public class ExistResult : AbstractStatus
		{
		}

		[DataContract]
		public class DelResult : AbstractStatus
		{
			[DataMember]
			public int delete_count;
		}

		#endregion [SubClass]

	}
}
